/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apps.controllers;

import com.jcraft.jsch.Channel;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 *
 * @author ARIJIT PAL
 */
public class CmdOutController implements Runnable {

    InputStream in;
    String endStr;
    String id;
    boolean flag;
    StringBuilder sb;
    Channel channel;

    public CmdOutController(Channel channel) throws Exception{
        this.in = channel.getInputStream();
        this.channel = channel;
        flag = false;
    }

    public void setListener(String endStr, String id){
        this.endStr = endStr;
        this.id = id;
        sb = new StringBuilder();
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
    
    @Override
    public void run() {
        try {
            sb = new StringBuilder();
            byte[] tmp = new byte[1024];
            while(true){
                while(in.available()>0){
                    int i = in.read(tmp, 0, 1024);
                    //if(i>=0)
                    //        System.out.println("HIT: "+endStr+" : "+new String(tmp, 0, i));
                    if(endStr != null && !endStr.equals("")){
                        sb.append(new String(tmp, 0, i));
                        if(sb.toString().contains(endStr)){
                            //save it to DB
                            String output = sb.toString();
                            String sql = "update commandio set output = '" + output
                                    + "', flag='O' where id='" + id + "' ";
                            System.out.println(sql);
                            DBController.updateData(sql);
                            endStr = "";
                            sb = new StringBuilder();
                        }else{
                            if(sb.toString().contains("Command not found")){
                                String output = "Command not found: "+sb.toString();;
                                String sql = "update commandio set output = '" + output
                                        + "', flag='O' where id='" + id + "' ";
                                System.out.println(sql);
                                DBController.updateData(sql);
                                endStr = "";
                                sb = new StringBuilder();
                            }
                        }
                        if (isFlag()) {
                            endStr = "";
                            sb = new StringBuilder();
                            break;
                        }
                    }else{
                        if(i>=0)
                            System.out.println(new String(tmp, 0, i));
                    }
                }
                if(channel.isClosed()){
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                }
                try{Thread.sleep(400);}catch(Exception ee){}
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(ex.getMessage());
        }
    }
}
